package com.qcxk.yuqian.system.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcxk.yuqian.system.entity.Dictionary;
import com.qcxk.yuqian.system.mapper.DictionaryMapper;
import com.qcxk.yuqian.system.service.DictionaryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DictionaryServiceImp extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public Page queryDictionaryPage(Page page, Dictionary entity) {

        QueryWrapper<Dictionary> dictionaryQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(entity.getSign())) {
            dictionaryQueryWrapper.eq("sign", entity.getSign());
        }

        if (StringUtils.isNotBlank(entity.getTextCn())) {
            dictionaryQueryWrapper.like("text_cn", entity.getTextCn());
        }
        if (StringUtils.isNotBlank(entity.getCode())) {
            dictionaryQueryWrapper.eq("code", entity.getCode());
        }

        dictionaryMapper.selectPage(page, dictionaryQueryWrapper);
        return page;
    }

    @Override
    public boolean updateDictionary(Dictionary entity) {
        Dictionary item = dictionaryMapper.selectById(entity.getId());
        if (StringUtils.isNotBlank(entity.getCode())) {
            item.setCode(entity.getCode());
        }
        if (StringUtils.isNotBlank(entity.getTextCn())) {
            item.setTextCn(entity.getTextCn());
        }

        if (StringUtils.isNotBlank(entity.getOther1())) {
            item.setOther1(entity.getOther1());
        }
        if (StringUtils.isNotBlank(entity.getOther2())) {
            item.setOther2(entity.getOther2());
        }
        if (StringUtils.isNotBlank(entity.getSign())) {
            item.setSign(entity.getSign());
        }

        int re = dictionaryMapper.updateById(item);
        return re == 1 ? true : false;
    }

    @Override
    public boolean deleteDictionaryById(Dictionary entity) {
        int re = dictionaryMapper.deleteById(entity.getId());
        if (re == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean chgHotwords(String hotwords) {

        QueryWrapper<Dictionary> dictionaryQueryWrapper = new QueryWrapper<>();
        dictionaryQueryWrapper.eq("sign", "hotwords");
        //删除原有热词
        dictionaryMapper.delete(dictionaryQueryWrapper);
        if (StringUtils.isNotBlank(hotwords)) {
            hotwords = hotwords.replaceAll("，", ",");
            String[] hotwordArr = hotwords.split(",");
            Dictionary dictionary;
            for (String item : hotwordArr) {
                dictionary = new Dictionary();
                dictionary.setSign("hotwords");
                dictionary.setTextCn(item);
                dictionaryMapper.insert(dictionary);
            }
        }
        return true;
    }
}
